<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input>
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 订单列表表格 -->
      <el-table :data="orderList" style="width: 100%" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="180px">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格">
        </el-table-column>
        <el-table-column prop="pay_status" label="是否付款">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.pay_status==='0'">未付款</el-tag>
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货">
        </el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">
            {{scope.row.create_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="editBtn"></el-button>
          <el-button type="success" icon="el-icon-location" size="mini" @click="progressBtn"></el-button>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[3, 5, 10, 15]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>

    <!-- 修改地址的对话框 -->
    <el-dialog title="修改地址" :visible.sync="addressVisible" width="50%" @close="addressClosed">
      <!-- 表单 -->
      <el-form :model="addressForm" :rules="addressFormRule" ref="addressFormRef" label-width="100px">
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader v-model="addressForm.address1" :options="cityDate" :props="{ expandTrigger: 'hover' }"
            @change="handleChange" class="inputValue">
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressOk">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 物流进度的对话框 -->
    <el-dialog title="物流进度" :visible.sync="progressVisible" width="50%">
      <!-- 时间线 -->
      <el-timeline>
        <el-timeline-item v-for="(activity, index) in activities" :key="index" :timestamp="activity.timestamp">
          {{activity.content}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>

  </div>
</template>

<script>
  import cityDate from './citydata'

  export default {
    data() {
      return {
        // 查询列表的参数
        queryInfo: {
          query: '',
          // 当前页码
          pagenum: 1,
          pagesize: 10,
        },
        // 订单列表
        orderList: [],
        // 订单总数
        total: 0,
        // 修改地址的对话框是否显示
        addressVisible: false,
        // 修改地址的 表单
        addressForm: {
          address1: [],
          address2: ""
        },
        // 表单规则
        addressFormRule: {
          address1: [{
            required: true,
            message: "请选择地址",
            trigger: "blur"
          }],
          address2: [{
            required: true,
            message: "请输入详细地址",
            trigger: "blur"
          }],
        },
        cityDate,
        // 物流进度对话框的显示与隐藏
        progressVisible: false,
        // 时间线的数据
        activities: [{
          content: '活动按期开始',
          timestamp: '2018-04-15'
        }, {
          content: '通过审核',
          timestamp: '2018-04-13'
        }, {
          content: '创建成功',
          timestamp: '2018-04-11'
        }]
      }
    },
    created() {
      this.getOrderList()
    },
    methods: {
      // 获取订单列表
      async getOrderList() {
        const {
          data: res
        } = await this.$http.get('orders', {
          params: this.queryInfo
        })
        if (res.meta.status !== 200) {
          return this.$message.error('获取订单列表失败')
        }
        this.orderList = res.data.goods
        this.total = res.data.total
      },
      // 分页 每页显示条数变化时触发
      handleSizeChange(newSize) {
        this.queryInfo.pagesize = newSize
        this.getOrderList()
      },
      // 分页 当前页面变化时触发
      handleCurrentChange(newPage) {
        this.queryInfo.pagenum = newPage
        this.getOrderList()
      },
      // 修改地址按钮
      editBtn() {
        this.addressVisible = true
      },
      // 级联选择器 节点发生变化时触发
      handleChange() {
        console.log(this.addressForm.address1);
      },
      // 修改地址的对话框关闭后
      addressClosed() {
        this.$refs.addressFormRef.resetFields()
      },
      // 地址修改后点击确定触发
      addressOk() {
        console.log(this.addressForm);
      },
      // 点击物流进度显示按钮
      async progressBtn() {
        // 获取物流信息
        this.progressVisible = true
      }
    }
  }

</script>

<style lang="less" scoped>
  .inputValue {
    width: 100%;
  }

</style>
